package com.inlycat.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.inlycat.entity.Video;
import com.inlycat.entity.VideoResp;
import com.inlycat.mapper.VideoAttrMapper;
import com.inlycat.mapper.VideoMapper;
import com.inlycat.mapper.VideoRespMapper;
import com.inlycat.service.VideoService;
import com.inlycat.utils.Constant;

@Service("videoService")
public class VideoServiceImpl implements VideoService {
	
	private static final Logger logger = LoggerFactory.getLogger(VideoServiceImpl.class);
	
	@Resource
	private VideoMapper videoMapper;
	
	@Resource
	private VideoAttrMapper videoAttrMapper;
	
	@Resource
	private VideoRespMapper videoRespMapper;

	@Override
	public boolean add(Video video) {
		/**
		 * 目前不做
		 */
		return false;
	}

	@Override
	public boolean deleteById(Long id) {
		/**
		 * 删除影片
		 */
		boolean result = videoMapper.deleteById(id);
		if(result){
			/**
			 * 删除影片的属性
			 */
			result = videoAttrMapper.deleteByVideoId(id);
			if(result){
				/**
				 * 删除影片责任人
				 */
				VideoResp videoResp = new VideoResp();
				videoResp.setVideoId(id);
				result = videoRespMapper.delete(videoResp);
			}
		}
		return result;
	}

	@Override
	public boolean update(Video video) {
		boolean result = videoMapper.updateSelective(video);
		return result;
	}

	@Override
	public Video selectDetailById(Long id) {
		Video video = videoMapper.selectDetailById(id);
		return video;
	}

	@Override
	public List<Video> list(String pageNum){
		int curPage = 1;
		try{
			curPage = Integer.parseInt(pageNum);
		}catch(NumberFormatException exception){
			logger.error(exception.getLocalizedMessage());
		}
		PageHelper.startPage(curPage, Constant.PAGE_SIZE, false);
		List<Video> videos = videoMapper.select();
		return videos;
	}

}
